﻿// Decompiled by AS3 Sorcerer 6.78
// www.buraks.com/as3sorcerer

//com.wdcgame.tile.display.TMXLayer

package com.game.tile.display
{
    import com.game.engine.phys.Vector2D;
    import com.game.tile.model.ImageLayerVO;
    import com.game.tile.model.LayerVO;
    import com.game.tile.model.TileLayerVO;
    import com.game.tile.model.Tileset;
    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.IOErrorEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.utils.Dictionary;
    
    public class TMXLayer extends Sprite
    {

        private var _type:String;
        private var _imgSource:String;
        private var _grid:Array;
        private var _layerWidth:int;
        private var _layerHeight:int;
        private var _path:String;
        private var _tilesets:Vector.<Tileset>;
        private var _onImageLoadComplete:Function;
        private var _properties:Dictionary;
        private var _itemIds:Array;
        private var _deleted:Boolean = false;
        private var _decorMc:MovieClip;
        private var _layerVo:LayerVO;
        private var _bp:Bitmap;
        private var _tiles:Vector.<Object>;
        private var _orientation:String;
        private var _bitmap:Bitmap;
        private var _bd:BitmapData;
        public var StartPositon:Vector2D;
        private var _bps:Vector.<Bitmap>;
        private var _moveInsideObj:Object;

        public function TMXLayer()
        {
            _itemIds = [];
            _tiles = new Vector.<Object>();
        }

        public function get layerVo():LayerVO
        {
            return (_layerVo);
        }

        public function get grid():Array
        {
            return (_grid);
        }

        public function get SpeeX():Number
        {
            if (this.layerVo.properties.hasOwnProperty("speedX"))
            {
                return (this.layerVo.properties["speedX"]);
            }
            return (0);
        }

        public function get SpeeY():Number
        {
            if (this.layerVo.properties.hasOwnProperty("speedY"))
            {
                return (this.layerVo.properties["speedY"]);
            }
            return (0);
        }

        public function buildFromLayerVo(_arg_1:LayerVO, _arg_2:Vector.<Tileset>, _arg_3:String):void
        {
            _orientation = _arg_3;
            _layerVo = _arg_1;
            _tilesets = _arg_2;
            if (_layerVo.type == "tile")
            {
                _grid = TileLayerVO(_layerVo).grid;
                if (name != "walkable")
                {
                    draw();
                }
            }
            else
            {
                if (_layerVo.type == "imagelayer")
                {
                    drawImage();
                }
            }
            this.visible = _arg_1.visible;
            this.alpha = _arg_1.opacity;
        }

        private function drawImage():void
        {
            var _local_4:int;
            var _local_1:* = null;
            if (_layerVo.properties.body == true)
            {
                return;
            }
            if (ImageLayerVO(_layerVo).bd)
            {
                this._bps = new Vector.<Bitmap>();
                _local_4 = 0;
                while (_local_4 < ImageLayerVO(_layerVo).repeat)
                {
                    _local_1 = new Bitmap(ImageLayerVO(_layerVo).bd, "auto", true);
                    addChild(_local_1);
                    _local_1.x = (_local_4 * _local_1.width);
                    this._bps.push(_local_1);
                    _local_4++;
                }
                return;
            }
            if (ImageLayerVO(_layerVo).displayObject)
            {
                addChild(ImageLayerVO(_layerVo).displayObject);
                return;
            }
            var _local_3:String = ImageLayerVO(_layerVo).source;
            if (_local_3 == "")
            {
                return;
            }
            var _local_2:Loader = new Loader();
            _local_2.load(new URLRequest(_local_3));
            _local_2.contentLoaderInfo.addEventListener("ioError", ioerr);
            addChild(_local_2);
        }

        protected function ioerr(_arg_1:IOErrorEvent):void
        {
            trace(_arg_1, this); //not popped
        }

        public function draw():void
        {
            var _local_1:int;
            var _local_8:int;
            var _local_5:int;
            var _local_14:* = null;
            var _local_12:* = null;
            var _local_7:Number;
            var _local_6:Number;
            var _local_10:* = null;
            var _local_9:* = null;
            var _local_11:* = null;
            var _local_2:Number = ((-(_grid[0].length) * this.layerVo.tileWidth) / 2);
            _local_2 = 0;
            var _local_3:Number = ((-(_grid.length) * this.layerVo.tileHeight) / 2);
            _local_3 = 0;
            _bd = new BitmapData((_grid[0].length * this.layerVo.tileWidth), (_grid.length * this.layerVo.tileHeight), true, 0);
            _bitmap = new Bitmap(_bd);
            addChild(_bitmap);
            var _local_13:int = this.layerVo.tileWidth;
            var _local_4:int = this.layerVo.tileHeight;
            _local_8 = 0;
            while (_local_8 < _grid.length)
            {
                _local_5 = 0;
                while (_local_5 < _grid[_local_8].length)
                {
                    _local_1 = _grid[_local_8][_local_5];
                    if (_local_1 != 0)
                    {
                        _local_14 = getTileset(_local_1);
                        if (_local_14.bitmapData == null)
                        {
                            throw (new Error(("找不到tileset图:" + _local_14.name)));
                        }
                        _local_12 = _local_14.bitmapData;
                        _local_7 = 0;
                        _local_6 = 0;
                        _local_10 = _local_14.getXY(_local_1);
                        _local_7 = _local_10.x;
                        _local_6 = _local_10.y;
                        _local_9 = new Rectangle(_local_7, _local_6, this.layerVo.tileWidth, this.layerVo.tileHeight);
                        _local_11 = new Point((_local_5 * _local_13), (_local_8 * _local_4));
                        _bd.copyPixels(_local_12, _local_9, _local_11);
                    }
                    _local_5++;
                }
                _local_8++;
            }
        }

        protected function drawTile(_arg_1:int):DisplayObject
        {
            var _local_4:Tileset = getTileset(_arg_1);
            if (_local_4.bitmapData == null)
            {
                throw (new Error(("找不到tileset图:" + _local_4.name)));
            }
            var _local_3:BitmapData = _local_4.getBitmapData(_arg_1);
            var _local_2:Bitmap = new Bitmap(_local_3);
            addChild(_local_2);
            return (_local_2);
        }

        private function getTileset(_arg_1:int):Tileset
        {
            var _local_3:int;
            var _local_2:* = null;
            _local_3 = 0;
            while (_local_3 < _tilesets.length)
            {
                _local_2 = _tilesets[_local_3];
                if (_local_2.contain(_arg_1))
                {
                    return (_local_2);
                }
                _local_3++;
            }
            return (_tilesets[0]);
        }

        public function get type():String
        {
            return (_layerVo.type);
        }

        public function get imgSource():Object
        {
            return (_imgSource);
        }

        public function get itemIds():Array
        {
            return (_itemIds);
        }

        public function get tilesets():Vector.<Tileset>
        {
            return (_tilesets);
        }

        public function get tiles():Vector.<Object>
        {
            return (_tiles);
        }

        protected function afterDrawTile(_arg_1:DisplayObject):void
        {
        }

        public function Update():void
        {
            var _local_2:* = null;
            var _local_4:int;
            var _local_1:* = null;
            var _local_3:int = -100;
            _local_4 = 0;
            while (_local_4 < this._bps.length)
            {
                _local_1 = this._bps[_local_4];
                _local_1.x = (_local_1.x + this.layerVo.properties.inside_speedX);
                _local_3 = Math.max((_local_1.x + _local_1.width), _local_3);
                if (_local_1.x <= -(_local_1.width))
                {
                    _local_2 = _local_1;
                }
                _local_4++;
            }
            if (_local_2)
            {
                _local_2.x = _local_3;
            }
        }

        public function MoveByInsideobj():void
        {
            var _local_3:int;
            var _local_1:* = null;
            var _local_2:* = null;
            if (this._moveInsideObj)
            {
                this._moveInsideObj.t = (this._moveInsideObj.t - GameFrame.PerTime);
                _local_3 = 0;
                while (_local_3 < this.numChildren)
                {
                    _local_1 = this.getChildAt(_local_3);
                    if (_local_1)
                    {
                        if ((_local_1 is GameObject))
                        {
                            _local_2 = GameObject(_local_1).Position;
                            _local_2.x = (_local_2.x + this._moveInsideObj.x);
                            _local_2.y = (_local_2.y + this._moveInsideObj.y);
                            GameObject(_local_1).setPosition(_local_2);
                        }
                        else
                        {
                            _local_1.x = (_local_1.x + this._moveInsideObj.x);
                            _local_1.y = (_local_1.y + this._moveInsideObj.y);
                        }
                    }
                    _local_3++;
                }
                if (this._moveInsideObj.t <= 0)
                {
                    this._moveInsideObj = null;
                }
            }
        }

        public function destroy():void
        {
            if (_deleted)
            {
                return;
            }
            _deleted = true;
            _layerVo.Destroy();
            if (((this.parent) && (this.parent.contains(this))))
            {
                this.parent.removeChild(this);
            }
        }

        public function MoveInsideBy(_arg_1:Number, _arg_2:Number, _arg_3:Number):void
        {
            this._moveInsideObj = {
                "x":_arg_1,
                "y":_arg_2,
                "t":_arg_3
            };
        }


    }
}//package com.wdcgame.tile.display

